<template>
  <div class="app-container">
    <!-- 设备table -->
    <el-card class="station-table-container">
      <el-form
        :model="queryParams"
        ref="queryForm"
        size="small"
        :inline="true"
        v-show="showSearch"
      >
        <el-form-item label="用户手机号" prop="phone">
          <el-input
            v-model="queryParams.phone"
            placeholder="请输入用户手机号"
            clearable
            style="width: 240px"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="el-icon-download" size="mini" @click="exportData"
            >导出</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置</el-button
          >
        </el-form-item>
      </el-form>

      <el-table v-loading="loading" :data="tableList">
        <el-table-column label="用户手机号" prop="phone" width="180" />
        <el-table-column label="总消费金额(元)" prop="amount">
          <template slot-scope="scope">
            <span>{{ scope.row.amount / 100 }}</span>
          </template>
        </el-table-column>
        <!-- <el-table-column label="次数卡使用(次)" prop="timesCardConsume" /> -->
        <el-table-column label="实体卡消费(元)" prop="orderAmountCard">
          <template slot-scope="scope">
            <span>{{ (scope.row.orderAmountCard || 0) / 100 }}</span>
          </template>
        </el-table-column>
        <el-table-column label="扫码充电金额(元)" prop="orderAmount">
          <template slot-scope="scope">
            <span>{{ scope.row.orderAmount / 100 }}</span>
          </template>
        </el-table-column>
        <el-table-column label="充电次数(次)" prop="chargeTimesCount" />
        <el-table-column label="充电时长(小时)" prop="duration" />
        <el-table-column label="用电量(度)" prop="chargeKWH">
          <template slot-scope="scope">
            <span>{{ scope.row.chargeKWH / 1000 }}</span>
          </template>
        </el-table-column>
        <el-table-column label="钱包余额(元)" prop="balance">
          <template slot-scope="scope">
            <span>{{ scope.row.balance / 100 }}</span>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
        :page-sizes="[10, 20, 30, 50, 500, 1000]"
      />
    </el-card>
  </div>
</template>

<script>
import { getSiteAnalysisAPI } from "@/api/charge/userAnalysis";
import { exportExcel, exportExcelFromAPI } from "@/utils/utils";

export default {
  name: "userAnalysis",
  dicts: ["sys_normal_disable"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 角色表格数据
      tableList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      editVisible: false,

      // 日期范围
      dateRange: [],

      deviceStatusMap: {
        NORMAL: "正常",
        BREAK: "故障",
      },
      onlineStatusMap: {
        ONLINE: "在线",
        OFFLINE: "离线",
        UNREGISTER: "未注册",
      },

      // 站点搜索关键词
      keywords: "",
      curSiteId: "",

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        phone: "",
      },
      // 表单参数
      form: {},

      stationData: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    exportData() {
      exportExcelFromAPI("/statistics/data/listChargerStatisticsList/export", {
        ...this.queryParams,
      });
      return false;
      exportExcel({
        data: this.formatTableList(this.tableList),
        headers: [
          { label: "用户手机号", key: "phone" },
          { label: "总消费金额(元)", key: "amount" },
          { label: "次数卡使用(次)", key: "timesCardConsume" },
          { label: "消费卡使用(元)", key: "orderAmountCard" },
          { label: "扫码充电金额(元)", key: "orderAmount" },
          { label: "充电次数(次)", key: "chargeTimesCount" },
          { label: "充电时长(小时)", key: "duration" },
          { label: "用电量(度)", key: "chargeKWH" },
          { label: "钱包余额(元)", key: "balance" },
        ],
        filename: "充电用户统计数据.xlsx",
      });
    },
    // 格式化表格数据
    formatTableList(data) {
      return data.map((item) => {
        return {
          ...item,
          amount: item.amount / 100,
          orderAmountCard: (item.orderAmountCard || 0) / 100,
          orderAmount: item.orderAmount / 100,
          chargeKWH: item.chargeKWH / 1000,
          balance: item.balance / 100,
        };
      });
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      getSiteAnalysisAPI({
        ...this.queryParams,
      }).then((res) => {
        this.tableList = res.rows;
        this.total = res.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
  },
};
</script>
<style scoped lang="scss">
.app-container {
  height: 100%;
  display: flex;

  .station-table-container {
    width: 100%;
  }
}
</style>
